package com.dfe.backend.controller;

import cn.hutool.core.util.StrUtil;
import com.dfe.backend.common.bean.real.cubicle.CubicleInfo;
import com.dfe.backend.common.bean.real.DeviceInfo;
import com.dfe.backend.common.cache.CaffeineCache;
import com.dfe.backend.common.sax.spd.SpdData;
import com.dfe.backend.common.sax.spd.substation.region.Cubicle;
import com.dfe.backend.common.sax.spd.substation.region.device.Device;
import com.dfe.backend.data.RtnData;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 【描述】：屏柜控制器
 *
 * @author LiChang
 * @since 2025-9-18 17:03
 */
@Slf4j
@RestController
@RequestMapping("/cubicle")
@RequiredArgsConstructor
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class CubicleController {

    CaffeineCache caffeineCache;

    /**
     * 【描述】: 获取全站屏柜拓扑
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getAllCubicleData")
    public RtnData getAllCubicleData() {
        // 获取SPD信息
        SpdData spdData = caffeineCache.spdDataCache().getIfPresent("spd");
        if (spdData != null) {
            return RtnData.ok(spdData.getRealData());
        }
        return RtnData.fail("SPD分析信息不存在！");
    }

    /**
     * 【描述】: 根据Key获取屏柜信息
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getCubicleInfo")
    public RtnData getCubicleInfo(@RequestParam String key) {
        // 获取SPD信息
        SpdData spdData = caffeineCache.spdDataCache().getIfPresent("spd");
        if (spdData != null) {
            Cubicle cubicle = spdData.getCubicleMap().get(key);
            if (cubicle != null) {
                // 获取屏柜详情
                CubicleInfo cubicleInfo = new CubicleInfo();
                cubicleInfo.setName(cubicle.getName());
                cubicleInfo.setDesc(cubicle.getDesc());
                List<DeviceInfo> iedList = new ArrayList<>();
                for (Device device : cubicle.getDeviceList()) {
                    if (StrUtil.isNotEmpty(device.getIedName())) {
                        iedList.add(DeviceInfo.build(device));
                    }
                }
                cubicleInfo.setIedList(iedList);
                return RtnData.ok(cubicleInfo);
            }
            return RtnData.ok("屏柜信息不存在！");
        }
        return RtnData.fail("SPD分析信息不存在！");
    }

    /**
     * 【描述】: 根据Key获取线缆详情
     *
     * @return {@link RtnData}
     * @author LiChang
     * @since 2025-8-8 9:00
     */
    @GetMapping("/getCableDetails")
    public RtnData getCableDetails(@RequestParam String key) {
        // 获取SPD信息
        SpdData spdData = caffeineCache.spdDataCache().getIfPresent("spd");
        if (spdData != null) {
            return RtnData.ok(spdData.getCableDetailsMap().get(key));
        }
        return RtnData.fail("SPD分析信息不存在！");
    }

}
